/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

jQuery(function($){

	$('.directedit-trigger').click(function(){

		if ( wgUserName == null ) {
			if ( confirm("Warning: You are not logged in. Your IP address will be recorded in the target page's edit history.") ) {
				handleDirectEdit( this );
			}
		} else {
			handleDirectEdit( this );
		}

		return false;
	});

	function handleDirectEdit( trigger ){
		var jtrigger = jQuery( trigger );
		var jdirectedit = jtrigger.closest( '.directedit' );
		var jresult = jdirectedit.find('.directedit-result');

		var reload = jtrigger.hasClass( 'reload' );

		var data = new Array();
		data.push( jdirectedit.find('form.directedit-data').serialize() );

		jtrigger.attr('class', 'directedit-trigger directedit-trigger-wait');
		jresult.attr('class', 'directedit-result directedit-result-wait');

		jresult[0].innerHTML="Wait..."; // TODO: replace by localized message

		sajax_request_type = 'POST';

		sajax_do_call( 'DirectEdit::handleDirectEdit', data, function( ajaxHeader ){
			jresult.empty().append( ajaxHeader.responseText );

			if ( ajaxHeader.status == 200 ) {

				if ( reload ) window.location.reload();

				jresult.removeClass('directedit-result-wait').addClass('directedit-result-ok');
				jtrigger.removeClass('directedit-trigger-wait').addClass('directedit-trigger-ok');
			} else {
				jresult.removeClass('directedit-result-wait').addClass('directedit-result-error');
				jtrigger.removeClass('directedit-trigger-wait').addClass('directedit-trigger-error');
			}
		} );
	}

})
